package DataStrucures;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree=new BinaryTree();
        Node root=new Node(1,"宋江");
        Node node2=new Node(2,"宋江");
        Node node3=new Node(3,"宋江");
        Node node4=new Node(4,"宋江");
        Node node5=new Node(5,"宋江");
        Node node6=new Node(6,"宋江");
        Node node7=new Node(7,"宋江");
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        binaryTree.setRoot(root);
        binaryTree.PreOrder();
        System.out.println();
        binaryTree.Del(3);
        binaryTree.PreOrder();



    }
}
class BinaryTree{
    private Node root;

    public void setRoot(Node root) {
        this.root = root;
    }

    public void PreOrder(){
        if(this.root!=null){
            this.root.preOrder();
        }
        else {
            System.out.println("二叉树为空");
        }

    }
    public void infixOrder(){
        if(this.root!=null){
            this.root.infixOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }
    public void postOrder(){
        if(this.root!=null){
            this.root.postOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }
    //查找节点的三种遍历方式
    public void preOrderSearch(int no){
        Node node=this.root.preOrderSearch(no);
        if(node==null){
            System.out.println("查无此节点");
        }else {
            System.out.println(node);
        }
    }
    public void infixOrderSearch(int no){
        Node node=this.root.infixOrderSearch(no);
        if(node==null){
            System.out.println("查无此节点");
        }else {
            System.out.println(node);
        }
    }
    public void postOrderSearch(int no){
        Node node=this.root.postOrderSearch(no);
        if(node==null){
            System.out.println("查无此节点");
        }else {
            System.out.println(node);
        }
    }
    public void Del(int no){
        if(root==null){
            System.out.println("空树无法删除");
            return;
        }
        if(root.getNo()==no){
            root=null;
            return;
        }
        root.Del(no);

    }
}
class Node{
    private int no;
    private String name;
    private Node left;
    private Node right;

    public Node(int no, String name) {
        super();
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    public void preOrder(){//前
        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }if(this.right!=null){
            this.right.preOrder();
        }
    }
    public void infixOrder(){//中
        if(this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixOrder();
        }
    }
    public void postOrder(){//后
        if(this.left!=null){
            this.left.postOrder();
        }
        if(this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }
    public Node preOrderSearch(int no){
        Node node=null;
        if(this.no==no){
            node=this;
            return node;
        }
        if(this.left!=null){
            node=this.left.preOrderSearch(no);
        }
        if(node!=null){
            return node;
        }
        if(this.right!=null){
            node=this.right.preOrderSearch(no);
        }
        return node;
    }
    public Node infixOrderSearch(int no){
        Node node=null;

        if(this.left!=null){
            node=this.left.infixOrderSearch(no);
        }
        if(node!=null){
            return node;
        }
        if(this.no==no){
            node=this;
            return node;
        }

        if(this.right!=null){
            node=this.right.infixOrderSearch(no);
        }
        return node;
    }
    public Node postOrderSearch(int no){
        Node node=null;

        if(this.left!=null){
            node=this.left.postOrderSearch(no);
        }
        if(node!=null){
            return node;
        }
        if(this.right!=null){
            node=this.right.postOrderSearch(no);
        }
        if(node!=null){
            return node;
        }
        if(this.no==no){
            node=this;
            return node;
        }
        return node;
    }
    public void  Del(int no){
       if(this.left!=null&&this.left.no==no){
           this.left=null;
           return;
       }
       if(this.right!=null&&this.right.no==no){
            this.right=null;
            return;
       }
       if(this.left!=null){
           this.left.Del(no);

       }
        if(this.right!=null){
            this.right.Del(no);
        }

    }

}
